package com.sale24h.data.adapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.example.sale24h.R;
import com.sale24h.common.ICallBack;
import com.sale24h.data.clsBan;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class BanAdapter extends ArrayAdapter<clsBan>
{
	private List<clsBan> lstBans;
	private Context context;
	private List<Integer> selectedItems;
	private ICallBack callbackobj;	
	private int itemPosition;

	public BanAdapter(Context context, int resource, List<clsBan> objects)
	{
		super(context, resource, objects);
		// TODO Auto-generated constructor stub
		this.context=context;
		this.lstBans=objects;
		this.selectedItems=new ArrayList<Integer>();	
		
	}
	
	public void setCallBack(ICallBack obj)
	{
		this.callbackobj=obj;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		this.itemPosition=position;
		LayoutInflater inflater = (LayoutInflater)this.context
		        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		View rootView=inflater.inflate(R.layout.ban_item_layout, parent,false);
		
		ImageView imgIcon=(ImageView)rootView.findViewById(R.id.imgIcon);
		TextView tbxMonAn_ten=(TextView)rootView.findViewById(R.id.tbxBan_ten);
		tbxMonAn_ten.setText(lstBans.get(position).getTenban());
		
		CheckBox chb=(CheckBox)rootView.findViewById(R.id.chbSelect);
		chb.setTag(lstBans.get(position).getId());
		
		chb.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
			{
				if(isChecked)
				{
					selectedItems.add((Integer)buttonView.getTag());
				}
				else
				{
					selectedItems.remove((Integer)buttonView.getTag());					
				}
				if(callbackobj!=null)
				{
					// call back to parent form to update action menu
					callbackobj.CallBack(selectedItems);					
				}
			}
		});
		
		
		rootView.setOnClickListener(new OnClickListener()
		{			
			@Override
			public void onClick(View v)
			{
				if (callbackobj!=null)
				{
					callbackobj.CallBack(itemPosition);	
				}
				
			}
		});
		return rootView;
	}
}
